#include<iostream>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<map>
#include<vector>
#include<cstring>
using namespace std;
typedef long long LL;
#define int long long
const int N = 2e5 + 10, mod = 998244353;
struct node
{
	int a, b, w;
}S[N];
int n, s;
int sz[N];
int p[N];
int cmp(node a, node b)
{
	return a.w < b.w;
}

int find(int x)
{
	if (x != p[x])p[x] = find(p[x]);
	return p[x];
}

int qpow(int a, int b)
{
	int ans = 1;
	while (b) {
		if (b & 1) {
			ans = ans * a % mod;
		}
		b >>= 1;
		a = a * a % mod;
	}
	return ans;
}
signed main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> s;
		for (int i = 1; i <= n; i++)p[i] = i, sz[i] = 1;
		for (int i = 1; i <= n - 1; i++) {
			int a, b, w;
			cin >> a >> b >> w;
			S[i] = { a,b,w };
		}
		sort(S + 1, S + n, cmp);
		int ans = 1;
		for (int i = 1; i <= n - 1; i++) {
			int a1 = find(S[i].a);
			int b1 = find(S[i].b);
			if (a1 != b1) {
				int s1 = sz[a1];
				int s2 = sz[b1];
				//cout<<"s1=="<<s1<<endl;
				//cout<<"s2=="<<s2<<endl;
				ans *= qpow(s - S[i].w + 1, s1 * s2 - 1);
				ans %= mod;
				//cout<<"ans=="<<ans<<endl;
				p[a1] = b1;
				sz[b1] += sz[a1];
			}
		}
		cout << ans << endl;
	}
	return 0;
}